package com.openlab.callsystem.controller;

import com.openlab.callsystem.entity.Caller;
import com.openlab.callsystem.entity.Log;
import com.openlab.callsystem.enums.BusinessType;
import com.openlab.callsystem.enums.LoginStatus;
import com.openlab.callsystem.service.CallerService;
import com.openlab.callsystem.service.LineUpService;
import com.openlab.callsystem.service.VerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/caller")
public class CallerController {
    @Autowired
    LineUpService lineup;
    @Autowired
    CallerService callerService;
    @Autowired
    VerifyService verifyService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/next")
    public Integer nextNumber(@RequestParam("windowsId")Integer windowsId){
        Integer businessType= (Integer) redisTemplate.opsForValue().get("business"+windowsId);
        return businessType.equals(BusinessType.Common.getValue())?lineup.CallCommonNumber(windowsId):lineup.CallVipNumber(windowsId);
    }
    @RequestMapping("/repeatcall")
    public void repeatCall(@RequestParam("number") Integer number,@RequestParam("windowsId")Integer windowsId){
        callerService.repeatCall(number,windowsId);
    }
    @RequestMapping("/login")
    public Integer login(HttpServletRequest request){
        //员工信息及窗口号

        String account=request.getParameter("account");
        String password=request.getParameter("password");
        Integer windowsId=Integer.parseInt(request.getParameter("windowsId")) ;
        Integer business= Integer.parseInt(request.getParameter("business"));
        Caller caller = callerService.login(account, password,windowsId);
        //判断

        if (caller!=null){

            //将请求信息保留到柜员的HttpSession中，传给CallerServiceServlet
            redisTemplate.opsForValue().set("business"+windowsId,business);
            redisTemplate.opsForValue().set("windowsId"+windowsId,caller.getEmail());
            redisTemplate.opsForValue().set("callerId"+windowsId,caller.getId());
            redisTemplate.opsForValue().set("callerName"+windowsId,caller.getName());
//            System.out.println(windowsId+"\t"+caller.getId()+"\t"+caller.getName());
            return LoginStatus.SUCCESS.getValue();
        }
        else {
            return LoginStatus.ERROR.getValue();
        }

    }
    @RequestMapping("/success/{number}")
    public String successs(
                           @PathVariable("number") Integer number,HttpServletRequest request,
                           @RequestParam("windowsId")Integer windows){
        HttpSession session = request.getSession();
        Integer callerId = (Integer) redisTemplate.opsForValue().get("callerId"+windows);
        String callerName= (String) redisTemplate.opsForValue().get("callerName"+windows);
        Integer windowsId= windows;
        Integer business= (Integer) redisTemplate.opsForValue().get("business"+windows);
        Log log=new Log(null,callerId,callerName,windowsId,business,1, LocalDate.now(),number);
        lineup.finishBussiness(log);
        return "success";
    }
    @PostMapping("/register")
    public String register(@RequestParam("username")String username,
                           @RequestParam("password")String password,
                           @RequestParam("email")String email,
                           @RequestParam("name")String name){
        boolean judge=callerService.registerCaller(username,name,password,email);
        if (judge){
            return "/mainindex";
        }else {
            return "/register";
        }
    }
    @RequestMapping("/query")
    public List<Caller> query(){
        return callerService.queryAllCallers();
    }
    @RequestMapping("/verifycode")
    public boolean verify(@RequestParam("code")Integer code,Integer windowsId){
        boolean result=verifyService.confirmCode(code,String.valueOf(windowsId));

        return result;
    }
}
